<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="../../common/includeBaseSet.jsp" %>
<%@include file="../../common/operation_forbid.jsp" %>
<%@include file="../../common/jsVersion.jsp"%>
<!DOCTYPE html >
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>客户详情</title>
    <!-- neptune -->
     <link rel="stylesheet" type="text/css"  href="${jypath}/static/ext/resources/ext-theme-gray/ext-theme-gray-all.css" />
     <script type="text/javascript" src="${jypath}/static/ext/ext-all.js"></script>
	 <script type="text/javascript" src="${jypath}/static/ext/locale/ext-lang-zh_CN.js"></script>
	 <script type="text/javascript" src="${jypath}/static/js/customer/customer/cusInfoLC.js?v=${jsVersion}"></script> 
	 <script type="text/javascript" src="${jypath}/static/js/customer/customer/cusFrom.js?v=${jsVersion}"></script> 
	 <script type="text/javascript" src="${jypath}/static/js/customer/customer/cusGou.js?v=${jsVersion}"></script>  
	 <script type="text/javascript" src="${jypath}/static/js/customer/customer/cusGen.js?v=${jsVersion}"></script>  
	 <script type="text/javascript" src="${jypath}/static/js/customer/customer/cusFang.js?v=${jsVersion}"></script>
	 <script type="text/javascript" src="${jypath}/static/js/customer/customer/cusSign.js?v=${jsVersion}"></script> 
	 <script type="text/javascript" src="${jypath}/static/js/components/callComponent.js?v=${jsVersion}"></script>
	 <script type="text/javascript">var customerId='${customerId}';</script>
	 <script type="text/javascript">var projectId='${projectId}';</script>
	 <script type="text/javascript">var resourcesCode='${resourcesCode}';</script>
	 <script type="text/javascript">
	 var groupStore = null;
	 var userStore = null;
	 var cusGenPanel = null;
	 var payDepositOff = ${payDepositOff};
	 var callPhoneOff = ${callPhoneOff};
	 var businessId = "${businessId}";
	 var intentionLevel = "";
	 var idata = '${permitBtn}';
	 if(idata!=null||idata!=""){
		idata=JSON.parse(idata);
	}
	 var queryButton = false;
	 if("${giveUp}"==1){
		 queryButton = true;
	 }
	 
	 var customerEdit = true ;
	 if(Ext.Array.contains(idata,"customer:customer:edit")){
  		customerEdit=false;
  	}
	 Ext.onReady(function() {
		 var cusInfoPanel=Ext.create('component.customer.cusInfoPanel');
		 var cusFormPanel=Ext.create('component.customer.cusFormPanel');
		 var cusGouPanel=Ext.create('component.customer.cusGouPanel');
		 var cusGenPanel=Ext.create('component.customer.cusGenPanel');
		 var cusFangPanel=Ext.create('component.customer.cusFangPanel');
		 var cusSignPanel=Ext.create('component.customer.cusSignPanel');
		  
		
		 
		 if(businessId != "" && businessId != "null"){
			 window.location.href="javascript:genJin("+customerId+", -1)";
		 }
		 
		 var followModesStoreList = [
		                     		{"value":"1","text":"来电"},
		                          	{"value":"2","text":"来访"},
		                          	{"value":"3","text":"信息"},
		                          	{"value":"4","text":"去电"},
		                          	{"value":"5","text":"拜访"},
		                         	{"value":"7","text":"微信"}
		                     	];
		 
		 followModesStore = Ext.data.Store({
			    autoDestroy: true,
			    fields :["value","text"],
			    data : followModesStoreList
			});
		 
		 groupStore = new Ext.data.Store({
			         fields: ["name", "id"],
			         proxy:{
			             type:'ajax',
			             url:jypath+'/customer/customer/returnGroupList',
			             reader:{
			                 type: 'json', 
			                 root: 'root'  //数据
			            }
			         }
			     });
		 
		groupStore.load({
 			params:{
 				'projectId':projectId
 			}
 		});
			     
	     userStore = new Ext.data.Store({
	         fields: ["userName", "id"],
	         proxy:{
	             type:'ajax',
	             url:jypath+'/customer/customer/returnGroupUserList',
	             reader:{
	                 type: 'json', 
	                 root: 'root'  //数据
	            }
	         }
	     });
		 
		 cusFormPanel.getComponent("gridStoreList1").getStore().load({
			 callback:function(){
				 rcTotal= cusFormPanel.getComponent("gridStoreList1").getStore().getTotalCount();
				 Ext.getCmp('rcTotal').setTitle("认筹（"+rcTotal+"）")
			 }
		 });
		 cusGouPanel.getComponent("gridStoreList2").getStore().load({
			 callback:function(){
				 rgTotal= cusGouPanel.getComponent("gridStoreList2").getStore().getTotalCount();
				 Ext.getCmp('rgTotal').setTitle("认购（"+rgTotal+"）")
			 }
		 });
		 cusSignPanel.getComponent("gridStoreList3").getStore().load({
			 callback:function(){
				 signTotal= cusSignPanel.getComponent("gridStoreList3").getStore().getTotalCount();
				 Ext.getCmp('signTotal').setTitle("签约（"+signTotal+"）")
			 }
		 });
		 
		 
		 var custemInfo3 = Ext.create('Ext.form.FormPanel',{
//			   frame: true,
			   minHeight:160,
			   collapsible: true,  //可折叠
		       autoScroll:false,
		       defaults: {
			    	 anchor: '100%',
			    },
			    fieldDefaults: {
			        margin: 2
			    },
		       title:'',
			   items:[{
					xtype : "container",
					layout : "hbox",
					items : [{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'经纪人',
				    	 name:'userName'
					},{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'创建人',
				    	 name:'createName'
					}]
				},{
					xtype : "container",
					layout : "hbox",
					items : [{
		                   width: '30%',
						   xtype:'displayfield',
				    	   fieldLabel:'项目名称',
				    	   name:'projectName'
				    	   
					},{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'客户等级',
				    	 name:'levelName'
					}]
				},{
					xtype : "container",
					layout : "hbox",
					items : [{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'客户姓名',
				    	 name:'customerName'
					},{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'客户状态',
				    	 name:'statusName'
					} ]
				},{
					xtype : "container",
					layout : "hbox",
					items : [{
						width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'性别',
				    	 name:'sexName'
					},{
						width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'获知途径',
				    	 name:'informedWayName',
				    	 id:'informedWay'
					}/* ,{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'客户性质',
				    	 name:'resourceName'
					}  */]
				},{
					xtype : "container",
					layout : "hbox",
					items : [{
						 xtype : "container",
				         layout : "hbox",
				         width: '30%',
				         items:[{
							 xtype:'displayfield',
					    	 fieldLabel:'联系电话',
					    	 name:'customerPhone',
					    	 id:'customer_phone'
						 },{
							 xtype:'button',
							 text: '拨打',
							 hidden:callPhoneOff,
							 icon : jypath+ '/resources/ExtJSicons/time_go.png',
							 handler: function() { 
								 var customerPhone=Ext.getCmp('customer_phone').getValue();
								 if(null==customerPhone || customerPhone==""){
									 Ext.MessageBox.alert("系统提示","客户联系电话为空"); 
									 return;
								 }
								 var calltWindow=Ext.create('component.phone.calltWindow',{
									 caller:customerPhone,
									 terminalModel:'0',
									 terminalModelId:customerId,
									 contextPath:jypath,
									 projectId:projectId,
									 callbackFun:function(uniqueId){
										 window.location.href="javascript:genJin("+customerId+","+uniqueId+")";
									 }
								 });
								 calltWindow.initRadioGroup();
							 }
						 }]
					}]
				},{
					xtype : "container",
					layout : "hbox",
					items : [{
						 width: '30%',
						 xtype:'displayfield',
				    	 fieldLabel:'意向等级备注',
				    	 cls: 'color: red;',
				    	 id:'intentionLevelRemark',
				    	 name:'intentionLevelRemark'
					}]
				}]
		   });

		custemInfo3.getForm().load({
			url:jypath+'/customer/customer/findCustomerInfo',
			params:{'customerId':customerId},
			success:function(form, action){
				var obj = action.result.data;
				intentionLevel = obj.intentionLevel;
			},
			failure: function(form, action) {
				Ext.Msg.alert("加载失败", action.result.errorMessage);
			}
		});
		 var item = Ext.create("Ext.form.Panel", {
			 minHeight:160,
			 renderTo: document.body,
			 items:[custemInfo3,tabs2],
			 tbar:[{ xtype: "button", text: "新增认筹", id:"addFrom", hidden: payDepositOff, icon : jypath+ '/resources/ExtJSicons/book_add.png',
	  	        	handler:function(){
	  	        		window.location.href="${jypath}/customer/payDeposit/toAdd?projectId="+projectId+"&customerId="+customerId+"&returnUrl=,customer,customer,toDetail(customerId)"+customerId+"&isChooseCustomer=true";
	  	        	}  
	  	        },
	  	        { xtype: "button", text: "新增认购",id:"addBuy",icon : jypath+ '/resources/ExtJSicons/bell_add.png',
	  	        	handler:function(){
	  	        		var url ="/customer/subscribe/toAdd?projectId="+projectId+"&customerId="+customerId+"&isChooseCustomer=true";
	  	        		parent.openMenu("0","subKey","","认购",url);
	  	        	}
	  	        },
	  	        { xtype: "button", text: "新增签约",id:"addSigned",icon : jypath+ '/resources/ExtJSicons/brick_add.png',
	  	        	handler:function(){
	  	        		var url ="/customer/signed/toAdd?projectId="+projectId+"&customerId="+customerId+"&isChooseCustomer=true";
	  	        		parent.openMenu("0","addSigned","","签约",url);
	  	        	}
	  	        },
	  	      	{ xtype: "button", text: "写跟进", id:"writeGT",icon : jypath+ '/resources/ExtJSicons/building_add.png',
				  	handler:function(){
				  		window.location.href="javascript:genJin("+customerId+",'-1',"+intentionLevel+")";
				  	}  
				  },
				{ xtype: "button", text: "分配", id:"distribute",icon : jypath+ '/resources/ExtJSicons/bullet_add.png',
				  	handler:function(){
				  		window.location.href="javascript:fenPei("+customerId+")";
				  	}  
				  },
				{ xtype: "button", hidden: customerEdit, text: "编辑", id:"edit",icon : jypath+ '/resources/ExtJSicons/cog_edit.png',
				  	handler:function(){
				  		var url = "/customer/customer/toUpdate?customerId="+customerId+"&1=1";
				  		parent.openMenu("0",customerId,"","编辑客户",url);
				  	}  
				},{xtype: "button",text: "返回",icon : jypath+ '/resources/ExtJSicons/bell_go.png',handler: function(){
					 var tabKey = "cus_"+customerId;
					 parent.TabControlRemove(tabKey);
		         }}]
		 })
		 var tabs2 = Ext.widget('tabpanel', {
			 	id:"tabpanel",
			  	layout : 'fit',
				renderTo: document.body,
		        activeTab: 0,
		        plain: true,
		        defaults :{
		            autoScroll: true,
		            bodyPadding: 10
		        },
		        items: [{
		                title: '客户资料',
		                items: cusInfoPanel
		            },{
		                title: '意向房源',
		                items: cusFangPanel
		            },{
		                title: '跟进记录',
		                items: cusGenPanel
		            },{
		            	id:"rcTotal",
		                title: "认筹",
		                items: cusFormPanel,
		                hidden: payDepositOff
		            },{
		            	id:"rgTotal",
		                title: '认购',
		                items: cusGouPanel
		            },{
		            	id:"signTotal",
		                title: '签约',
		                items: cusSignPanel
		            }
		        ], 
                listeners:{ 
                    tabchange:function(tp,p){ 
                        if(p.id=='rcTotal'){ 
//                         	Ext.getCmp('addBuy').setVisible(false);
//                         	Ext.getCmp('addSigned').setVisible(false);
//                         	Ext.getCmp('addFrom').setVisible(true);
                        }else if(p.id=='rgTotal'){ 
//                         	Ext.getCmp('addFrom').setVisible(false);
//                         	Ext.getCmp('addSigned').setVisible(false);
//                         	Ext.getCmp('addBuy').setVisible(true);
                        }else if(p.id=='signTotal'){ 
//                         	Ext.getCmp('addFrom').setVisible(false);
//                         	Ext.getCmp('addBuy').setVisible(false);
//                         	Ext.getCmp('addSigned').setVisible(true);
                        } else if(p.title=='客户资料'||p.title=='意向房源'||p.title=='跟进记录'){ 
//                         	Ext.getCmp('addFrom').setVisible(false);
//                         	Ext.getCmp('addBuy').setVisible(false);
//                         	Ext.getCmp('addSigned').setVisible(false);
                        }
                        
                    } 
                }
		    });
		 //已放弃客户跳转详情，跳转 没有操作按钮
		 if("${giveUp}"==1){
			 Ext.getCmp("addBuy").setVisible(false);
			 Ext.getCmp("addSigned").setVisible(false);
			 Ext.getCmp("writeGT").setVisible(false);
			 Ext.getCmp("distribute").setVisible(false);
			 Ext.getCmp("edit").setVisible(false);
		 }
		 
	 })

function genJin(id, followId, intentionLevel){
    var wjtBunHidden=false;
    if(followId<=0){
    	wjtBunHidden=true;
    }

    var intentionLevelStoreList = [
        {"value":"2","text":"A"},
        {"value":"3","text":"B"},
        {"value":"4","text":"C"},
        {"value":"5","text":"D"}
    ];
    var intentionLevelStoreList2 = [
        {"value":"3","text":"B"},
        {"value":"4","text":"C"},
        {"value":"5","text":"D"}
    ];
    var intentionLevelStore = Ext.data.Store({
        autoDestroy: true,
        fields :["value","text"],
        data : intentionLevelStoreList
    });
    var intentionLevelStore2 = Ext.data.Store({
        autoDestroy: true,
        fields :["value","text"],
        data : intentionLevelStoreList2
    });

	var fromPanel =   Ext.create("Ext.form.Panel", {
		collapsible : false, 
		autoScroll : false, 
		defaultType : 'textfield',
		defaults : {
			anchor : '100%'
		},
		fieldDefaults : {
			flex : 1
		},
		items : [{
            xtype:'combobox',
            id:'intentionLevel',
            store: intentionLevelStore,
            displayField: "text",
            valueField: "value",
            fieldLabel:'<font style="color:#e90000;">*</font>意向等级',
            typeAhead: true,
            name:'intentionLevel',
            allowBlank:true,
            blankText:'不能为空',
            listeners: {
                afterRender: function(combo) {
                    if(intentionLevel == "2"){
                        combo.setReadOnly(true);
                    }else{
                        combo.bindStore(intentionLevelStore2);
                    }
                    combo.setValue(intentionLevel + '');
                }
            },
            typeAhead:false,
            editable: false
        },{
	            xtype:'combobox',  
	            id:'followMode',  
	            store: followModesStore,
	            displayField: "text",           
	            valueField: "value",  
	            fieldLabel:'<font style="color:#e90000;">*</font>跟进方式',  
	            width:200,
	            typeAhead: true,
    			name:'followMode',
                allowBlank:false,
                blankText:'不能为空'
	        },
            { 
	            xtype:'textarea',
    			name:'followContents',  
	            fieldLabel:'<font style="color:#e90000;">*</font>跟进内容',
                allowBlank:false,
                blankText:'不能为空'
    		},
            { 
	            xtype: "container",
	            layout: "hbox",
	            items: [
	                    { xtype: "datefield",id:"nextFollowQ", name: "nextFollowQ",fieldLabel: "下次提醒时间",format:'Y-m-d',minWidth:200},
	                    { xtype: "timefield",id:"nextFollowH", name: "nextFollowH",format:'G:i',minWidth:100}
	            ]
    		},
            { 
	            xtype:'textarea',
    			name:'nextFollowContent',  
	            fieldLabel:'下次提醒内容'
    		},
            { 
	            xtype:'hiddenfield',
    			name:'followId',
    			value:followId
    		}
		]
	});
	var infoPanel =   Ext.create("Ext.form.Panel", {
		collapsible : false, 
		autoScroll : false, 
		layout: {
	        align: 'middle',
	        pack: 'center' ,
	        type: 'hbox' 
		},
		items:[/* {
				 xtype:'displayfield',
				 value: '温馨提示：记得完善客户信息哦！',
			}, */{
				 hidden:wjtBunHidden,
				 xtype:'displayfield',
				 value: '注：点击【未接通】按钮将结束通话，并自动生成跟进记录。',
			}
		]
	});
	
	var window = Ext.create('Ext.Window', {
        title: "新增跟进",
        width: 500,
        height: 350,
        plain: true,
        headerPosition: 'top',
        items: [fromPanel,infoPanel],
		buttons:[{hidden:wjtBunHidden,xtype: "button",text: "未接通",icon : jypath+'/resources/ExtJSicons/exclamation.png',handler: function(){
					Ext.Ajax.request({
			       			url: jypath+'/customer/follow/edit',
			       			params:{
			       				id:followId,
			       				followContent:'【去电未接通】'
			       			},
			       			callback: function (options, success, response) {
			       				if (success) {
			       					var obj = JSON.parse(response.responseText);
			       					if(obj.res=="1"){
			       						Ext.MessageBox.alert("系统提示","操作成功"); 
			       						window.close();
			       					}else{
			       						Ext.MessageBox.alert("系统提示",obj.resMsg); 
			       					}
			       				}else{
			       					Ext.MessageBox.alert("系统提示","系统异常!");
			       				}
			       			}
			       		
			       	});
		         }},
		         {xtype: "button",text: "保存",icon : jypath+'/resources/ExtJSicons/computer_add.png',handler: function(){
			     			var form = fromPanel.getForm();
//			     	       	var nextFollowQ=form.findField("nextFollowQ").getValue();
			     	       	var nextFollowH=form.findField("nextFollowH").getValue();
			     	       	if(form.isValid()){
			     	       		Ext.Ajax.request({
			     	       			//+'&nextFollowQ='+nextFollowQ
			     	       			url: jypath+'/customer/customer/addFollow?customerId='+id+'&projectId='+projectId+'&nextFollowH='+nextFollowH,
			     	       			params:form.getValues(),
			     	       			callback: function (options, success, response) {
			     	       				if (success) {
			     	       					var obj = JSON.parse(response.responseText);
			     	       					if(obj.res=="1"){
			     	       						Ext.MessageBox.alert("系统提示","写跟进成功"); 
			     	       					    window.close();
			     	       						
			     	       					}else{
			     	       						Ext.MessageBox.alert("系统提示",obj.resMsg); 
			     	       					}
			     	       				}else{
			     	       					Ext.MessageBox.alert("系统提示","系统异常!");
			     	       				}
			     	       			}
			     	       		
			     	       		});
			     	       	}
		         }},{
		        	 xtype: "button",text: "取消" ,icon : jypath+'/resources/ExtJSicons/cancel.png',handler:function(){
		        		 window.close();
		        	 }
		         }]
    });
	window.show();
}

function fenPei(id){
	var fromPanel =   Ext.create("Ext.form.Panel", {
		collapsible : false, 
		autoScroll : false, 
		defaultType : 'textfield',
		defaults : {
			anchor : '100%'
		},
		fieldDefaults : {
			flex : 1
		},
		items : [{
	            xtype:'combobox',  
	            id:'oo',  
	            store: groupStore,
	            displayField: "name",           
	            valueField: "id",  
	            fieldLabel:'销售组名称',  
	            width:200,
	            listeners:{
	            	'change':function(value){
	            		Ext.getCmp('userId').setValue("");
	            		userStore.load({
		            			params:{
		            				'salesGroupId':value.lastValue
		            			}
		            		});
	            	}
	            },
	            name:'projectId',
    			queryMode : 'local' 
	        },
            { 
	            xtype:'combobox',  
	            id:'userId',  
	            store: userStore,
	            displayField: "userName",           
	            valueField: "id",  
	            fieldLabel:'经纪人',
	            width:200,
    			name:'consultantUserId',
    			queryMode : 'local' 
    		}
		]
	});
	var window = Ext.create('Ext.Window', {
        title: "分配",
        width: 300,
        height: 150,
        x: 10,
        y: 200,
        plain: true,
        headerPosition: 'top',
        items: [fromPanel],
		buttons:[
		         {xtype: "button",text: "保存",iconCls:'icon-save',handler: function(){
			     			var form = fromPanel.getForm();
			     			Ext.Ajax.request({
			     				url: jypath+'/customer/customer/distributionConsultant?customerId='+id,
			     				params:form.getValues(),
			     				callback: function (options, success, response) {
			     					 if (success) {
			     						 var obj = JSON.parse(response.responseText);
			     						 if(obj.res=="1"){
			     							Ext.MessageBox.alert("系统提示","分配成功"); 
			     							window.close();
			     						 }else{
			     							Ext.MessageBox.alert("系统提示",obj.resMsg); 
			     						 }
			     		        	 }else{
			     		        		 Ext.MessageBox.alert("系统提示","系统异常!");
			     		        	 }
			     				}
			     				
			     			});
		         }}]
    });
	window.show();
}

function genJinList(id,type){

	var gridStoreg_1 = Ext.create('Ext.data.Store', {
        fields: ["id","smallUrl","createTime"],
        pageSize: 10,  //页容量5条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/customer/customer/imageList?objectId='+id+'&imgType=4',
            actionMethods: { read: 'POST' },
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            }
        },
        autoLoad: true  //即时加载数据
    });
	var gridg = Ext.create('Ext.grid.Panel', {
		minHeight:600,
        store: gridStoreg_1,
        columnLines: true,
        columns: [{
            text: '创建日期',
            width:'30%',
            dataIndex: 'createTime',
            renderer:function(value){
            	return Ext.Date.format(new Date(value),'Y-m-d');
            }
        },{
            text     : '附件类型',
            dataIndex: 'id',
            width:'20%',
            renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
                var rec = gridg.getStore().getAt(rowIndex);
            	if(type==2){
                	return "图片";
                }else if(type==3){
                	return "语音";
                }
            }
        },{
            text     : '操作',
            dataIndex: 'smallUrl',
            width:'50%',
            renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
            	if(type==2){
            		var html = '<a href="javascript:previewImg(\''+value+'\')">预览</a>';
                    return html;
            	}else if(type==3){
            	    var html='<audio controls="controls"> <source src="'+value+'" type="audio/mpeg"> </audio>'
            	    return html;
            	}
            }
        }
        ],
        //title: '附件列表',
        dockedItems: [{  
            xtype: 'pagingtoolbar',  
            store: gridStoreg_1,   // GridPanel中使用的数据  
            dock: 'bottom',  
            displayInfo: true,  
            emptyMsg:"没有数据",  
            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据"
        }
        ]
    });
	var window = Ext.create('Ext.Window', {
        title: "附件列表",
        width: 650,
        height: 300,
        //plain: true,
        modal : true, 
        headerPosition: 'top',
        items: [gridg]
    });
	window.show();
}

function previewImg(url){
	var imgs="<img style='width:auto;height:100%;margin:0 auto;' src='"+url+"' />";
	var window = Ext.create('Ext.Window', {
        title: "图片预览",
        width: '100%',
        height: '100%',
        autoScroll:true,
        maximizable:false,
        layout : 'fit', 
        html:imgs
    });
	window.show();
}

	 </script>
</head>
<body>
</body>
</html>